package com.atguigu.lease.web.admin.custom.convert;

import com.atguigu.lease.model.enums.BaseEnum;
import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;

/**
 * @ClassName StringToBaseEntityConverFactory
 * @Author heartFade
 * @Date 2025-05-01 21:27
 * @Version 1.0
 * @Description TODO
 **/
@Component
public class StringToBaseEnumConverterFactory implements ConverterFactory<String , BaseEnum> {
    @Override
    public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {

        return new Converter<String, T>() {
            @Override
            public T convert(String source) {
                T[] enumConstants = targetType.getEnumConstants();
                for(T e:enumConstants)
                {
                    if(String.valueOf(e.getCode()).equals(source))
                    {
                        return e;
                    }
                }
                return null;
            }
        };
    }
}
